package com.lys.cloud.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.lys.cloud.dto.UserInfo;


@RestController
public class UserController {

//	@PostMapping("/users")
//	public UserInfo createUser(@RequestBody UserInfo user) {
//		System.out.println("create user, sex:" + user.getSex()+ ", name:" + user.getName());
//		user.setId(1l);
//		return user;
//	}
	
	@PostMapping(value = "/users")
	public ResponseEntity<Void> createUser(@RequestBody final UserInfo user,
			final UriComponentsBuilder ucBuilder) {
		
		user.setId(1l);
		
		HttpHeaders headers = new HttpHeaders();
		headers.setLocation(ucBuilder.path("/users/{id}").buildAndExpand(user.getId()).toUri());
		
		return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
	}
	
	@DeleteMapping("/users/{id}")
	public void deleteUser(@PathVariable long id) {
		System.out.println("delete user, id is:" + id);
	}
	
	@PutMapping("/users/{id}")
	public UserInfo update(@PathVariable long id, @RequestBody UserInfo user) {
		System.out.println(user.getId());
		System.out.println(user.getName());
		System.out.println(user.getPassword());
		user.setId(1l);
		return user;
	}
	
	@PatchMapping(value = "/users/{id}")
	public UserInfo patchUser(@PathVariable("id") long id, @RequestBody UserInfo user) {
		System.out.println("user sex:" + user.getSex());
		System.out.println("user name:" + user.getName());
		return user;
	}
	
	@GetMapping(value = "/users")
	public List<UserInfo> listAllUserInfo() {
		
		List<UserInfo> users = new ArrayList<UserInfo>();
		
		UserInfo user1 = new UserInfo();
		user1.setId(1l);
		user1.setName("name1");
		user1.setSex("male");
		
		UserInfo user2 = new UserInfo();
		user2.setId(2l);
		user2.setName("name2");
		user2.setSex("female");
		
		users.add(user1);
		users.add(user2);
		
		return users;
	}
	
	/*
	@RequestMapping(value = "/users", method = RequestMethod.GET) 
	public ResponseEntity<List<UserInfo>> listAllUserInfo() {
		
		List<UserInfo> users = new ArrayList<UserInfo>();
		
		UserInfo user1 = new UserInfo();
		user1.setId(1l);
		user1.setName("name1");
		user1.setSex("male");
		
		UserInfo user2 = new UserInfo();
		user2.setId(2l);
		user2.setName("name2");
		user2.setSex("female");
		
		users.add(user1);
		users.add(user2);
		
		return new ResponseEntity<List<UserInfo>>(users, HttpStatus.OK);
	}
	*/
}
